/**
 * Connects to the database and updates entries depending on parameters.
 * 
 * @author Ludvig Gee
 */
package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class editDb {
    Connection con;
    String edit_1, edit_2, edit_3, edit_4, edit_5, edit_6, edit_7;
    String restID, optionEdit, dishAla, dishLunch;
    int editDone;

    public editDb() {

    }

    public editDb(String restID) {

	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	} catch (Exception e) {
	    System.out.println(e);
	    System.out.println("bajs");
	}

    }

    /**
     * Executes different updates on different tables depending on what value
     * optionEdit has and then inserts in specific row if edit parameter has a
     * value.
     * 
     * @param restID
     * @param optionEdit
     *            string derived from previous option string from OwnerDb.
     * @param dishAla
     *            string that represents a la carte menu dish
     * @param dishLunch
     *            string that represents lunch menu dish
     * @param edit_1
     *            input from form in edit.jsp
     * @param edit_2
     *            input from form in edit.jsp
     * @param edit_3
     *            input from form in edit.jsp
     * @param edit_4
     *            input from form in edit.jsp
     * @param edit_5
     *            input from form in edit.jsp
     * @param edit_6
     *            input from form in edit.jsp
     * @param edit_7
     *            input from form in edit.jsp
     */
    public void editUpdate(String restID, String optionEdit, String dishAla,
	    String dishLunch, String edit_1, String edit_2, String edit_3,
	    String edit_4, String edit_5, String edit_6, String edit_7) {

	if (optionEdit.equals("1")) {

	    if (edit_1.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.menu SET menu.dish= '"
			    + edit_1 + "' WHERE menu.rest_id= '" + restID
			    + "' AND menu.dish='" + dishAla + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_2.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.menu SET menu.description= '"
			    + edit_2 + "' WHERE menu.rest_id= '" + restID
			    + "' AND menu.dish='" + dishAla + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_3.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.menu SET menu.Price= '"
			    + edit_3 + "' WHERE menu.rest_id= '" + restID
			    + "' AND menu.dish='" + dishAla + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_4.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.menu SET menu.Type= '"
			    + edit_4 + "' WHERE menu.rest_id= '" + restID
			    + "' AND menu.dish='" + dishAla + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	}

	if (optionEdit.equals("2")) {

	    if (edit_1.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.ownerlogin SET ownerlogin.Owner= '"
			    + edit_1
			    + "' WHERE ownerlogin.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_2.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.ownerlogin SET ownerlogin.username= '"
			    + edit_2
			    + "' WHERE ownerlogin.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_3.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.ownerlogin SET ownerlogin.password= '"
			    + edit_3
			    + "' WHERE ownerlogin.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	}

	if (optionEdit.equals("3")) {

	    if (edit_1.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.lunch SET lunch.date= '"
			    + edit_1 + "' WHERE lunch.rest_id= '" + restID
			    + "' AND lunch.dish='" + dishLunch + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_2.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.lunch SET lunch.dish= '"
			    + edit_2 + "' WHERE lunch.rest_id= '" + restID
			    + "' AND lunch.dish='" + dishLunch + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_3.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.lunch SET lunch.description= '"
			    + edit_3
			    + "' WHERE lunch.rest_id= '"
			    + restID
			    + "' AND lunch.dish='" + dishLunch + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_4.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.lunch SET lunch.price= '"
			    + edit_4 + "' WHERE lunch.rest_id= '" + restID
			    + "' AND lunch.dish='" + dishLunch + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_5.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.lunch SET lunch.type= '"
			    + edit_5 + "' WHERE lunch.rest_id= '" + restID
			    + "' AND lunch.dish='" + dishLunch + "'";
		    editDone = 11;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	}

	if (optionEdit.equals("4")) {

	    if (edit_1.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.type= '"
			    + edit_1
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_2.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.adress= '"
			    + edit_2 + "' WHERE menu.rest_id= '" + restID + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_3.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.description= '"
			    + edit_3
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_4.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.phone_nr= '"
			    + edit_4
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	}

	if (optionEdit.equals("5")) {

	    if (edit_1.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.monday= '"
			    + edit_1
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_2.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.tuesday= '"
			    + edit_2
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_3.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.wednesday= '"
			    + edit_3
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_4.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.thursday= '"
			    + edit_4
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_5.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.friday= '"
			    + edit_5
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	    if (edit_6.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.saturday= '"
			    + edit_6
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }
	    if (edit_7.length() > 0) {

		try {
		    Statement stmt = con.createStatement();
		    String query = "UPDATE olofek.restaurant SET restaurant.sunday= '"
			    + edit_7
			    + "' WHERE restaurant.rest_id= '"
			    + restID
			    + "'";
		    editDone = 1;
		    stmt.executeUpdate(query);
		} catch (Exception e) {
		    System.out.println(e);
		}
	    }

	}
    }

    public String getRestID() {
	return restID;
    }

    /**
     * parameter to signify successful edit.
     * 
     * @return int set depending if query is made.
     */
    public int getEditDone() {
	return editDone;
    }
}